using UnityEngine;
using System.Collections;

public class Control : MonoBehaviour {
	public GameObject obj;
	public GameObject robj;
	public int state=1,temp,n=0;
	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
		objRotate();
		objMove ();
	}

//    ,·'´¨;.  '                          , ·. ,.-·~·.,   ‘        ,.-.                                 _,.,  °    
//    ;   ';:\           .·´¨';\         /  ·'´,.-·-.,   `,'‚       /   ';\ '                       ,.·'´  ,. ,  `;\ '  
//   ;     ';:'\      .'´     ;:'\       /  .'´\:::::::'\   '\ °    ';    ;:'\      ,·'´';          .´   ;´:::::\`'´ \'\  
//   ;   ,  '·:;  .·´,.´';  ,'::;'    ,·'  ,'::::\:;:-·-:';  ';\‚     ';   ;::;     ,'  ,''\        /   ,'::\::::::\:::\:' 
//  ;   ;'`.    ¨,.·´::;'  ;:::;    ;.   ';:::;´       ,'  ,':'\‚    ';   ';::;   ,'  ,':::'\'     ;   ;:;:-·'~^ª*';\'´   
//  ;  ';::; \*´\:::::;  ,':::;‘     ';   ;::;       ,'´ .'´\::';‚    ';   ;:;  ,'  ,':::::;'     ;  ,.-·:*'´¨'`*´\::\ '  
// ';  ,'::;   \::\;:·';  ;:::; '     ';   ':;:   ,.·´,.·´::::\;'°     ;   ;:;'´ ,'::::::;'  '   ;   ;\::::::::::::'\;'   
// ;  ';::;     '*´  ;',·':::;‘        \·,   `*´,.·'´::::::;·´        ';   '´ ,·':::::;'        ;  ;'_\_:;:: -·^*';\   
// \´¨\::;          \¨\::::;          \\:¯::\:::::::;:·´            ,'   ,.'\::;·´          ';    ,  ,. -·:*'´:\:'\° 
//  '\::\;            \:\;·'            `\:::::\;::·'´  °             \`*´\:::\;     ‘        \`*´ ¯\:::::::::::\;' '
//    '´¨               ¨'                  ¯                         '\:::\;'                  \:::::\;::-·^*'´     
//                                          ‘                           `*´‘                     `*´¯	
	void objMove() {
		if(state==1){	
        	if(!Physics.Raycast(robj.transform.position, robj.transform.TransformDirection(Vector3.down),5))
        		if(Input.GetKeyDown(KeyCode.DownArrow))
					obj.transform.Translate(new Vector3(0,-10,0));
        
			if(!Physics.Raycast(transform.position, transform.TransformDirection(Vector3.up), 5))
				if(Input.GetKeyDown(KeyCode.UpArrow))
					obj.transform.Translate(new Vector3(0,10,0));
		
			if(!Physics.Raycast(transform.position, transform.TransformDirection(Vector3.right), 5))
				if(Input.GetKeyDown(KeyCode.RightArrow))
					obj.transform.Translate(new Vector3(10,0,0));
		
			if(!Physics.Raycast(transform.position, transform.TransformDirection(Vector3.left), 5))
				if(Input.GetKeyDown(KeyCode.LeftArrow))
					obj.transform.Translate(new Vector3(-10,0,0));
		}
		
		if(state==2){
        	if (!Physics.Raycast(robj.transform.position, robj.transform.TransformDirection(Vector3.down),5))
        		if(Input.GetKeyDown(KeyCode.RightArrow))
					obj.transform.Translate(new Vector3(0,-10,0));
        
			if (!Physics.Raycast(transform.position, transform.TransformDirection(Vector3.up), 5))
				if(Input.GetKeyDown(KeyCode.LeftArrow))
					obj.transform.Translate(new Vector3(0,10,0));
		
			if (!Physics.Raycast(transform.position, transform.TransformDirection(Vector3.right), 5))
				if(Input.GetKeyDown(KeyCode.UpArrow))
					obj.transform.Translate(new Vector3(10,0,0));
		
			if (!Physics.Raycast(transform.position, transform.TransformDirection(Vector3.left), 5))
				if(Input.GetKeyDown(KeyCode.DownArrow))
					obj.transform.Translate(new Vector3(-10,0,0));
		}
	
		
		if(state==3){
        	if (!Physics.Raycast(robj.transform.position, robj.transform.TransformDirection(Vector3.down),5))
        		if(Input.GetKeyDown(KeyCode.UpArrow))
					obj.transform.Translate(new Vector3(0,-10,0));
        
			if (!Physics.Raycast(transform.position, transform.TransformDirection(Vector3.up), 5))
				if(Input.GetKeyDown(KeyCode.DownArrow))
					obj.transform.Translate(new Vector3(0,10,0));
		
			if (!Physics.Raycast(transform.position, transform.TransformDirection(Vector3.right), 5))
				if(Input.GetKeyDown(KeyCode.LeftArrow))
					obj.transform.Translate(new Vector3(10,0,0));
		
			if (!Physics.Raycast(transform.position, transform.TransformDirection(Vector3.left), 5))
				if(Input.GetKeyDown(KeyCode.RightArrow))
					obj.transform.Translate(new Vector3(-10,0,0));
		}
		
		if(state==4){			
        	if (!Physics.Raycast(robj.transform.position, robj.transform.TransformDirection(Vector3.down),5))
        		if(Input.GetKeyDown(KeyCode.LeftArrow))
					obj.transform.Translate(new Vector3(0,-10,0));
        
			if (!Physics.Raycast(transform.position, transform.TransformDirection(Vector3.up), 5))
				if(Input.GetKeyDown(KeyCode.RightArrow))
					obj.transform.Translate(new Vector3(0,10,0));
		
			if (!Physics.Raycast(transform.position, transform.TransformDirection(Vector3.right), 5))
				if(Input.GetKeyDown(KeyCode.DownArrow))
					obj.transform.Translate(new Vector3(10,0,0));
		
			if (!Physics.Raycast(transform.position, transform.TransformDirection(Vector3.left), 5))
				if(Input.GetKeyDown(KeyCode.UpArrow))
					obj.transform.Translate(new Vector3(-10,0,0));
		}
	}
	
	
	
	
	
	
//     ,. -  .,                     , ·. ,.-·~·.,   ‘             ,  . .,  °                    ,.,   '               ,  . .,  °                   _,.,  °    
//   ,' ,. -  .,  `' ·,             /  ·'´,.-·-.,   `,'‚       ;'´    ,   ., _';\'                ;´   '· .,        ;'´    ,   ., _';\'          ,.·'´  ,. ,  `;\ '  
//   '; '·~;:::::'`,   ';\         /  .'´\:::::::'\   '\ °     \:´¨¯:;'   `;::'\:'\             .´  .-,    ';\      \:´¨¯:;'   `;::'\:'\       .´   ;´:::::\`'´ \'\  
//    ;   ,':\::;:´  .·´::\'    ,·'  ,'::::\:;:-·-:';  ';\‚       \::::;   ,'::_'\;'            /   /:\:';   ;:'\'      \::::;   ,'::_'\;'      /   ,'::\::::::\:::\:' 
//    ;  ·'-·'´,.-·'´:::::::';  ;.   ';:::;´       ,'  ,':'\‚          ,'  ,'::;'  ‘            ,'  ,'::::'\';  ;::';          ,'  ,'::;'  ‘       ;   ;:;:-·'~^ª*';\'´   
//  ;´    ':,´:::::::::::·´'    ';   ;::;       ,'´ .'´\::';‚         ;  ;:::;  °        ,.-·'  '·~^*'´¨,  ';::;          ;  ;:::;  °       ;  ,.-·:*'´¨'`*´\::\ '  
//   ';  ,    `·:;:-·'´         ';   ':;:   ,.·´,.·´::::\;'°         ;  ;::;'  ‘         ':,  ,·:²*´¨¯'`;  ;::';          ;  ;::;'  ‘       ;   ;\::::::::::::'\;'   
//   ; ,':\'`:·.,  ` ·.,         \·,   `*´,.·'´::::::;·´            ;  ;::;'‚           ,'  / \::::::::';  ;::';          ;  ;::;'‚         ;  ;'_\_:;:: -·^*';\   
//   \·-;::\:::::'`:·-.,';        \\:¯::\:::::::;:·´               ',.'\::;'‚          ,' ,'::::\·²*'´¨¯':,'\:;           ',.'\::;'‚         ';    ,  ,. -·:*'´:\:'\° 
//    \::\:;'` ·:;:::::\::\'       `\:::::\;::·'´  °                 \::\:;'‚          \`¨\:::/          \::\'            \::\:;'‚          \`*´ ¯\:::::::::::\;' '
//     '·-·'       `' · -':::''          ¯                             \;:'      ‘       '\::\;'            '\;'  '           \;:'      ‘        \:::::\;::-·^*'´     
//                                     ‘                               °                `¨'                                °                 `*´¯              	
	void objRotate() {
		transform.rotation = Quaternion.Lerp(transform.rotation,Quaternion.Euler(0,0,n),10*Time.deltaTime);
		if(Input.GetKeyDown(KeyCode.X)) {
			if(!Physics.Raycast(transform.position,Vector3.right,5)&&!Physics.Raycast(transform.position,Vector3.left,5)){
				n+=90;
				state+=1;
			}
			if(state>4)
				state=1;
		}	
		if(Input.GetKeyDown(KeyCode.Z)) {
			n-=90;
			state-=1;
			if(state<1)
				state=4;
		}
	}
}
